package com.soushin.chinachess.utils;

import com.soushin.chinachess.App;
import com.soushin.chinachess.widget.game.GameConfig;

import android.content.Context;
import android.content.SharedPreferences;



/**
 * 缓存用户信息
 * @auther SouShin
 * @time 2018/2/2 10:30
 **/

public class SharedUtils {
    public static String filename="chess_cache";
	public static String pref_level_key="computer_power";
	public static String pref_sound_key="switch_sound";
	public static String pref_handicap_key="handicap";
	public static String pref_who_first_key="switch_computer_first";
	public static String pref_piece_style_key="list_chess_style";
	private static int computer_power;
	private static int switch_sound;
	private static int handicap;
	private static int switch_computer_first;
	private static int list_chess_style;
	private static String last_fen;

    public static void initData(Context context){
    	setComputer_power(getIntParam(context, pref_level_key));
    	setSwitch_sound(getIntParam(context, pref_sound_key));
    	setHandicap(getIntParam(context, pref_handicap_key));
    	setSwitch_computer_first(getIntParam(context, pref_who_first_key));
    	setList_chess_style(getIntParam(context, pref_piece_style_key));
    	setLast_fen(getStringParam(context,GameConfig.PREF_LAST_FEN));
    }
 
    public static boolean setIntParam(Context context, String key, int value) {

        try {
            SharedPreferences preferences = context.getSharedPreferences(filename, context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt(key, value);
            return editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static int getIntParam(Context context, String key) {
        try {
            SharedPreferences preferences = context.getSharedPreferences(filename, context.MODE_PRIVATE);
            return preferences.getInt(key, 0);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static boolean setStringParam(Context context, String key, String value) {

        try {
            SharedPreferences preferences = context.getSharedPreferences(filename, context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(key, value);
            return editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String getStringParam(Context context, String key) {

        try {
            SharedPreferences preferences = context.getSharedPreferences(filename, context.MODE_PRIVATE);
            return preferences.getString(key, "");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    /**
     * 清除缓存的数据
     * @return
     */
    public static Boolean clearCache(){
        SharedPreferences preferences = App.getInstance().getSharedPreferences(filename, App.getInstance().MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        return editor.commit();
    }

	public static int getComputer_power() {
		return computer_power;
	}
	public static void setComputer_power(int computer_power) {
		if(setIntParam(App.getInstance(), pref_level_key, computer_power)) {
			SharedUtils.computer_power = computer_power;
		}
	}

	public static int getSwitch_sound() {
		return switch_sound;
	}

	public static void setSwitch_sound(int switch_sound) {
		if(setIntParam(App.getInstance(), pref_sound_key, switch_sound)) {
			SharedUtils.switch_sound = switch_sound;
		}
	}

	public static int getHandicap() {
		return handicap;
	}

	public static void setHandicap(int handicap) {
		if(setIntParam(App.getInstance(), pref_handicap_key, handicap)) {
			SharedUtils.handicap = handicap;
		}
	}

	public static int getSwitch_computer_first() {
		return switch_computer_first;
	}

	public static void setSwitch_computer_first(int switch_computer_first) {
		if(setIntParam(App.getInstance(), pref_who_first_key, switch_computer_first)) {
			SharedUtils.switch_computer_first = switch_computer_first;
		}
	}

	public static int getList_chess_style() {
		return list_chess_style;
	}

	public static void setList_chess_style(int list_chess_style) {
		if(setIntParam(App.getInstance(), pref_piece_style_key, list_chess_style)) {
			SharedUtils.list_chess_style = list_chess_style;
		}
	}
	public static String getLast_fen() {
		return last_fen;
	}

	public static void setLast_fen(String last_fen) {
		if(setStringParam(App.getInstance(), GameConfig.PREF_LAST_FEN, last_fen)) {
			SharedUtils.last_fen = last_fen;
		}
	}

}
